//trainingservice_list_ctrl
function trainingservice_list_ctrl()
{
    $("#trainingservice_list").show();
    return false;
}

function trainingservice_list_elemt()
{
    $("#trainingservice_name").val($(this).text());
    $("#trainingservice_id").val($(this).attr("id"));
    $("#trainingservice_list").hide();
}

function bid_submit()
{
    var gid = $("#gid").val();
    var subject = $("#subject").val();
    var contact_phone = $("#contact_phone").val();
    //var session_id = $("#session_id").val();
    var session_cnt = $("#session_cnt").val();
    var free_session_cnt = $("#free_session_cnt").val();
    var trainingservice_id = $("#trainingservice_id").val();
    var bid_price = $("#bid_price").val();
    var session_id = "1";
    var message = $("#message").val();
    var bid = $("#bid").val();
    if (gid == 0) {
	alert("Please select the goal.");
	return false;
    }
    if (subject == "") {
	alert("Subject line is empty.");
	return false;
    }
    if (subject.length > 50) {
	alert("Subject line is too long.");
	return false;
    }
    if (contact_phone == "") {
	alert("Phone number is empty.");
	return false;
    }
//    if (is_numberic(contact_phone) == false) {
//	alert("Phone number format is not valid");
//	return false;
//    }
    if (session_id == 0 || is_numberic(session_id) == false) {
	alert("Please select length of session.");
	return false;
    }
    if (trainingservice_id == 0 || is_numberic(trainingservice_id) == false) {
	alert("Please select length of training type.");
	return false;
    }
    if (is_numberic(session_cnt) == false) {
	alert("Paid session number is not valid.");
	return false;
    }
    if (is_numberic(free_session_cnt) == false) {
	alert("Free session number is not valid.");
	return false;
    }
    if (is_numberic(bid_price) == false) {
	alert("Bid price is not valid.");
	return false;
    }
    if (message == "") {
	alert("Message is empty.");
	return false;
    }
    if (message.length > 2000) {
	alert("Message is too long.");
	return false;
    }
    var data = {
	gid : gid,
	subject : subject,
	contact_phone : contact_phone,
	trainingservice_id : trainingservice_id,
	session_id : session_id,
	session_cnt : session_cnt,
	free_session_cnt : free_session_cnt,
	bid_price : bid_price,
	message: message,
	bid:bid
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/goal/dobid',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Bid submitted successfully");
		window.location.href="/goal/detail/gid/"+gid;
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return true;
}
function award_submit()
{
    var ret = confirm("Are you sure you want to award this trainer?");
    
    if (ret == false) {
        return false;
    }
    var gid = $(this).attr("attr_gid");
    var bid = $(this).attr("attr_bid");
    var data = {
	gid : gid,
	bid : bid
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/goal/award',
	success : function(msg){
	    if (msg.status == 0) {
		alert("You have successfully awarded this trainer. ");
		window.location.href="/user/dashboard";
	    }
	    else {
		alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false; 
}

function accept_submit()
{
    var gid = $(this).attr("attr_gid");
    var bid = $(this).attr("attr_bid");
    var data = {
	gid : gid,
	bid : bid
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/goal/accept',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Accept award success.");
		window.location.href="/user/dashboard";
	    }
	    else {
		alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false; 
}


function review_submit()
{
    var gid = $(this).attr("attr_gid");
    var bid = $(this).attr("attr_bid");
    var score = 0;
    var clss = $("#star").attr("class");
    if (clss == "s_0") {
        alert("Please score the trainer.");
        return false;
    }
    if (clss == "s_1") {
        score = 1;
    }
    else if (clss == "s_2") {
        score = 2;
    }
    else if (clss == "s_3") {
        score = 3;
    }
    else if (clss == "s_4") {
        score = 4;
    }
    else if (clss == "s_5") {
        score = 5;
    }
    var content = $("#content").val();
    if (content == "") {
	alert("Review content is empty.");
	return false;
    } 
    if (content.length > 2000) {
	alert("Review content is too long.");
	return false;
    } 
    var data = {
	gid : gid,
	bid : bid,
	score : score,
	content : content
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/goal/review',
	success : function(msg){
	    if (msg.status == 0) {
		alert("Review success.");
		window.location.href="/user/dashboard";
	    }
	    else {
		alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
    return false; 
}

function init_star()
{
    var is_click = false;
    $("#star .show_1").mouseover(function(){
		$("#star").removeClass().addClass("s_1");
		}).mouseout(function(){
			if (!is_click){
                $("#star").removeClass().addClass("s_0");
            }
			}).click(function(){
				$("#star").removeClass().addClass("s_1");
                is_click=true;
            });
	$("#star .show_2").mouseover(function(){
		$("#star").removeClass().addClass("s_2")
		}).mouseout(function(){
			if (!is_click){
                $("#star").removeClass().addClass("s_0");
            }
			}).click(function(){
				$("#star").removeClass().addClass("s_2");
                is_click=true;
			})
	$("#star .show_3").mouseover(function(){
		$("#star").removeClass().addClass("s_3")
		}).mouseout(function(){
			if (!is_click){
                $("#star").removeClass().addClass("s_0");
            }
			}).click(function(){
				$("#star").removeClass().addClass("s_3");
                is_click=true;
			})
	$("#star .show_4").mouseover(function(){
		$("#star").removeClass().addClass("s_4")
		}).mouseout(function(){
			if (!is_click){
                $("#star").removeClass().addClass("s_0");
            }
			}).click(function(){
				$("#star").removeClass().addClass("s_4");
                is_click=true;
			})
	$("#star .show_5").mouseover(function(){
		$("#star").removeClass().addClass("s_5")
		}).mouseout(function(){
			if (!is_click){
                $("#star").removeClass().addClass("s_0");
            }
			}).click(function(){
				$("#star").removeClass().addClass("s_5");
                is_click=true;
			})
}

$(function(){
    $("#trainingservice_list_ctrl").click(trainingservice_list_ctrl);
    $("#trainingservice_name").click(trainingservice_list_ctrl);
    $("#trainingservice_block").mouseleave(function(){$("#trainingservice_list").hide();});
    $("#trainingservice_list").find("li").each(function(){$(this).click(trainingservice_list_elemt);});

    $("#bid_submit").click(bid_submit);

    $("#award_submit").click(award_submit);
    $("#accept_submit").click(accept_submit);
    $("#review_submit").click(review_submit);
    init_star();
});
